/**
 * 功能 把数据转成适合listSelection组件格的（只针对特定数据式式--私有工具）
 *var data={"id":"4","parentId":"-1","children":[{"id":"1970","parentId":"4","children":[],"name":"永升物业太仓分公司","orgLevel":2,"projects":null,"order":3}],"name":"上海永升物业管理股份有限公司","orgLevel":0,"projects":null,"order":1}
 *
 *func(data) 即可返回合法的
 * @Author: lizuping
 * @Date: 2018-00-00
 * @Last Modified by: lizuping
 * @Last Modified time: 2018-07-23 19:44:17
 */

export default function (data, nameKey, nodeKey) {
    nameKey = nameKey || 'deptName';
    nodeKey = nodeKey || 'memberNodes';
    var put = { title: '无', listData: [] };
    if (data.length === 1) {
        put.title = data[0][nameKey];
    } else {
        data = [{children: data}];
    }
    let listData = [];
    hasOrg(data[0], listData);
    var len = listData.length;
    for (var i = 0;i < len;i++) {
        var tempObj = { name: '', show: false, child: [] };
        tempObj.name = listData[i][nameKey];
        tempObj.child = listData[i][nodeKey];
        put.listData.push(tempObj);
    }
    return put;
    function hasOrg (d, p) {
        if (d[nodeKey] && d[nodeKey].length > 0) {
            p.push(d);
        }
        if (d.children && d.children.length > 0) {
            for (let i = 0;i < d.children.length;i++) {
                d.children[i][nameKey] = d[nameKey] + '/' + d.children[i][nameKey];
                hasOrg(d.children[i], p);
            }
        }
    }
}
